﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;
using XISoft.AppCreator.Util;
using XISoft.AppCreator.Util.Common;

namespace XISoft.AppCreator.CommonUI.UML
{
    public partial class Item : UserControl
    {
        private const int _height = 24;
        public Item()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            size = this.Size;
            this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            #region 静态测试
            _classson = ClassSon.Attribute;
            _DrawString = "Attribute";
            //_classson = ClassSon.Mothod;
            //_DrawString = "Mothod";
            //_classson = ClassSon.Filed;
            //_DrawString = "Filed";
            //_classson = ClassSon.Event;
            //_DrawString = "Event";
            #endregion
        }
        private ClassSon _classson;
        private Size size = new Size();
        private string _DrawString=string.Empty;
        private bool _isSelected = false;
        private ItemPanel _itempannel = null;
        private int id = 0;

        public Item(ClassSon classson, ItemPanel itempannel,int Id)
        {
            InitializeComponent(); 
            this.DoubleBuffered = true;
            
            _classson = classson;
            switch (_classson)
            {
                case ClassSon.Event:
                    _DrawString = "Event";
                    break;
                case ClassSon.Method:
                    _DrawString = "Method";
                    break;
                case ClassSon.Attribute:
                    _DrawString = "Attribute";
                    break;
                case ClassSon.Filed:
                    _DrawString = "Field";
                    break;
            } _itempannel = itempannel;

            this.Width = itempannel.Width;
            this.Size = new System.Drawing.Size(_itempannel.Width, _height);
            this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                         | System.Windows.Forms.AnchorStyles.Right)));
            id = Id;
            if (_DrawString.IndexOf(':') < 0)
                _DrawString += " : string";

        }

        public Item(ClassSon classson, string caption, ItemPanel itempannel,int Id)
        {
            InitializeComponent(); this.DoubleBuffered = true;
             _classson = classson;
            _DrawString = caption;
            _itempannel = itempannel;
            this.Width = itempannel.Width;
            this.Size = new System.Drawing.Size(_itempannel.Width, _height);
            this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                         | System.Windows.Forms.AnchorStyles.Right)));

            id = Id;
            if (_DrawString.IndexOf(':') < 0)
                _DrawString += " : string";
            
        }
        private void Item_Load(object sender, EventArgs e)
        {


            this.Height = _height;
            size = this.Size;
        }
        private void Item_Paint(object sender, PaintEventArgs e)
        {
            switch (_classson)
            {
                case ClassSon.Attribute:
                    e.Graphics.DrawImage(itemList.Images[0], new Point(27, -1));
                    break;
                case ClassSon.Filed:
                    e.Graphics.DrawImage(itemList.Images[1], new Point(27, -1));
                    break;
                case ClassSon.Method:
                    e.Graphics.DrawImage(itemList.Images[2], new Point(27, -1));
                    break;
                case ClassSon.Event:
                    e.Graphics.DrawImage(itemList.Images[3], new Point(27, -1));
                    break;
            }
            e.Graphics.DrawString(_DrawString, new Font(new FontFamily("宋体"), 9), new SolidBrush(Color.Black), new PointF(50, 2));
            if (_isSelected)
            {
                Pen selPen=new Pen(Color.Gray);
                selPen.DashStyle=DashStyle.Dot;
                e.Graphics.DrawRectangle(selPen,new Rectangle(0,0,this.Width-1,this.Height-1));
                e.Graphics.DrawString(_DrawString, new Font(new FontFamily("宋体"), 9), new SolidBrush(Color.White), new PointF(50, 2));
            }
        }
        private void Item_Click(object sender, EventArgs e)
        {
          
        }
 
        private void EditorName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                _DrawString = ((TextBox)sender).Text;
                ItemChange(this, new EventArgs());
                this.Refresh();
                this.Size = size;
                this._isSelected = true;
                this.Controls.Clear();
                this.Refresh();
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Size = size;
                this._isSelected = true;
                this.Controls.Clear();
                this.Refresh();
            }
        }

        private void Item_Leave(object sender, EventArgs e)
        {
            if (this.Controls.Count >0)
            {
                _DrawString = ((TextBox)Controls[0]).Text;
                this.Refresh();
                this.Size = size;
                for(int i=Controls.Count-1;i>=0;i--)
                {
                    Control c = Controls[i];
                    Controls.RemoveAt(i);
                    c.Dispose();
                }

                this.Controls.Clear();

                if (_DrawString.IndexOf(':') < 0)
                    _DrawString += " : string";

                ItemChange(this, new EventArgs());
            }

            this._isSelected = false;
            this.BackColor = Color.White;
            this.Refresh();
        }

        private void Item_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                _itempannel.Remove(this);
                this.Dispose();
                
            }
            if (e.KeyCode == Keys.Down)
            {
                int index = this._itempannel.Controls.IndexOf(this);
                if (index < this._itempannel.Controls.Count - 1)
                {
                    this._itempannel.Controls[index + 1].Focus();
                }
                else
                {
                    _itempannel.Controls[0].Focus();
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                int index = this._itempannel.Controls.IndexOf(this);
                if (index > 0)
                {
                    this._itempannel.Controls[index - 1].Focus();
                }
                else
                {
                    _itempannel.Controls[_itempannel.Controls.Count - 1].Focus();
                }
            }
        }

        private void Item_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Focus();
                if (!_isSelected)
                {
                    if (this.Controls.Count == 0)
                    {
                        this.BackColor = Color.DarkBlue; _isSelected = true;
                    }
                }
                else
                {
                    _isSelected = false;
                    if (id == 0) return;
                    TextBox textBox = new TextBox();
                    textBox.Text = _DrawString;
                    textBox.Font = new Font(new FontFamily("宋体"), 12);
                    textBox.Location = new Point(50, 0);
                    this.Controls.Add(textBox);
                    //textBox.Height = _height; // this.Height;
                    textBox.Width = this.Width;
                    this.BringToFront();
                    textBox.KeyDown += new KeyEventHandler(EditorName_KeyDown);
                }
            }
        }

        private void Item_Enter(object sender, EventArgs e)
        {
            this.BackColor = Color.DarkBlue;
        }

        private void popDelete_Click(object sender, EventArgs e)
        {
            String name=_DrawString.Split(':')[0].Trim();
            foreach (DataRow dr in (this.Parent.Parent as ClassObject).entity.Rows)
            {
                if (Equals(dr[ModelDataDictionary.EntityTable.Name], name))
                {
                    if (dr[ModelDataDictionary.EntityTable.isPrimaryKey].Equals(false))
                    {
                        _itempannel.Remove(this);
                        this.Dispose();
                    }
                    else
                        MessageBox.Show("主键不能删除！");
                    break;
                }
            }
        }

        private void Item_Resize(object sender, EventArgs e)
        {
            size = this.Size;
            if (Controls.Count > 0)
                Controls[0].Width = Width;
        }


        #region property
        public ClassSon ClassSon
        {
            get { return _classson; }
        }

        public new String Name
        {
            get {  return _DrawString.Split(':')[0].Trim(); }
            set {
                if (value.IndexOf(':') < 0)
                    _DrawString = value + " : " + Type;
                else
                    _DrawString = value;
            }
        }

        public String Type
        {
            get {
                String[] tmp = _DrawString.Split(':');
                if (tmp.Length <= 1)
                    return String.Empty;
                else
                return tmp[1].Trim(); }
            set {
                if (value.IndexOf(':') < 0)
                    _DrawString = Name + " : " + value;
                else
                    _DrawString = value;
            }
        }
        public int ID
        {
            get { return id; }
        }
        #endregion


        public event EventHandler ItemChange;

    }
}
